package com.oig.sys.base.controller;

import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.BankDto;
import com.oig.sys.base.service.ITbBankService;
import com.oig.sys.base.vo.BankVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "银行管理")
@RestController
@RequestMapping("/bank")
@Slf4j
public class TbBankController {

    private final ITbBankService bankService;

    @Autowired
    public TbBankController(ITbBankService bankService){
        this.bankService = bankService;
    }

    @PreAuthorize("@pms.hasPermission('/bank/addBank')")
    @SysLog(value="添加银行",apiUrl="/bank/addBank")
    @Operation(summary = "添加银行")
    @PostMapping(value = "/addBank")
    public BankVo addBank(@RequestBody @Validated(GroupAdd.class)  BankDto BankDto){
        log.info("add Bank {}",BankDto);
        return this.bankService.addTbBank(BankDto);
    }

    @PreAuthorize("@pms.hasPermission('/bank/updateBank')")
    @SysLog(value="修改银行",apiUrl="/bank/updateBank")
    @Operation(summary = "修改银行")
    @PostMapping(value = "/updateBank")
    public BankVo updateBank(@RequestBody @Validated(GroupEdit.class)  BankDto BankDto){
        log.info("update Bank {}",BankDto);
        return this.bankService.updateTbBank(BankDto);
    }

    @PreAuthorize("@pms.hasPermission('/bank/cancelBank')")
    @SysLog(value="逻辑删除银行",apiUrl="/bank/cancelBank")
    @Operation(summary = "逻辑删除银行")
    @PostMapping(value = "/cancelBank/{bankCode}")
    public SysResponse<String> cancelBank(@PathVariable("bankCode") String bankCode){
        log.info("cancel Bank code={}", bankCode) ;
        this.bankService.cancelTbBank(bankCode);
        return SysResponse.ok(bankCode,"逻辑删除银行成功");
    }


    @Operation(summary = "查询备选银行")
    @PostMapping(value = "/findBank")
    public SysPageInfo<BankVo> findBank(@RequestBody SysRequest<BankDto> bankRequest){
        log.info("find bank {}", bankRequest) ;
        return this.bankService.findBank(bankRequest) ;
    }

}
